package com.yzs.exam.config.spring.mvc;

import com.yzs.exam.config.property.SystemConfig;
import com.yzs.exam.config.spring.vx.TokenHandlerInterceptor;
import lombok.AllArgsConstructor;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.context.annotation.Configuration;
import org.springframework.web.servlet.config.annotation.*;

import java.util.List;

/**
 * webmvc配置，把拦截器定义到mvc配置文件中
 * @author yzs
 * @create 2021-03-23 16:35
 */
@Configuration
public class WebMvcConfiguration extends WebMvcConfigurationSupport {
    private final TokenHandlerInterceptor tokenHandlerInterceptor;
    private final SystemConfig systemConfig;

    @Autowired
    public WebMvcConfiguration(TokenHandlerInterceptor tokenHandlerInterceptor, SystemConfig systemConfig) {
        this.tokenHandlerInterceptor = tokenHandlerInterceptor;
        this.systemConfig = systemConfig;
    }

    //配置多个路由 -- 重定向
    @Override
    public void addViewControllers(ViewControllerRegistry registry) {
        registry.addRedirectViewController("/", "/user/index.html");
        registry.addRedirectViewController("/user", "/user/index.html");
        registry.addRedirectViewController("/admin", "/admin/index.html");
    }

    //静态资源映射 --
    @Override
    public void addResourceHandlers(ResourceHandlerRegistry registry) {
        //拦截/下的所有路径
        //映射到真实位置
        //将静态资源进行缓存 31556926ms
        registry.addResourceHandler("/**")
                .addResourceLocations("classpath:/static/")
                .setCachePeriod(31556926);
        registry.addResourceHandler("swagger-ui.html").addResourceLocations("classpath:/META-INF/resources/");
        registry.addResourceHandler("doc.html").addResourceLocations("classpath:/META-INF/resources/");
        registry.addResourceHandler("/webjars/**").addResourceLocations("classpath:/META-INF/resources/webjars/");
    }

    //添加拦截器 -- 对/api/wx/** 进行拦截
    @Override
    public void addInterceptors(InterceptorRegistry registry) {
        List<String> securityIgnoreUrls = systemConfig.getWx().getSecurityIgnoreUrls();
        String[] ignores = new String[securityIgnoreUrls.size()];
        registry.addInterceptor(tokenHandlerInterceptor)
                .addPathPatterns("/api/wx/**")
                .excludePathPatterns(securityIgnoreUrls.toArray(ignores))//防止swagger资源被拦截
                .excludePathPatterns(
                        "/swagger-resources/**",
                        "/webjars/**", "/v2/**",
                        "/swagger-ui.html/**",
                        "/webjars/springfox-swagger-ui",
                        "/webjars/springfox-swagger-ui/**");
        super.addInterceptors(registry);
    }

    //跨域处理：*代表允许所有
    @Override
    public void addCorsMappings(CorsRegistry registry) {
        registry.addMapping("/**")
                .allowCredentials(true)
                .allowedMethods("*")
                .allowedOrigins("*")
                .allowedHeaders("*");
        super.addCorsMappings(registry);
    }
}
